package cn.chinaunicom.sdsi.upfc_demp.dzgh.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.chinaunicom.sdsi.frm.base.BaseService;
import cn.chinaunicom.sdsi.frm.sys.R;
import cn.chinaunicom.sdsi.frm.sys.entity.SysStaff;
import cn.chinaunicom.sdsi.frm.sys.entity.SysStaffOrg;
import cn.chinaunicom.sdsi.upfc_demp.dzgh.dao.mybatis.DzghApplyMapper;
import cn.chinaunicom.sdsi.upfc_demp.dzgh.entity.DzghApplyEntity;
import cn.chinaunicom.sdsi.upfc_demp.dzgh.service.DzghApplyService;
import cn.chinaunicom.sdsi.upfc_demp.jyxx.entity.JyXx;
import net.sf.json.JSONObject;

@Service
public class DzghApplyServiceImpl extends BaseService implements DzghApplyService {
	@Autowired
	private DzghApplyMapper dzghApplyMapper;

	@Override
	public DzghApplyEntity getSessionStaff() {
		DzghApplyEntity bean = new DzghApplyEntity();
		// 获取登录用户的session信息
		SysStaffOrg so = (SysStaffOrg) this.getSessionCache(R.UserSign.SIGNIN_USER_STAFFORG_SESSION);
		SysStaff staff = (SysStaff) getSessionCache(R.UserSign.SIGNIN_USER_SESSION);
		// bean.setOrg_id(so.getOrgId().toString());
		// 获取信息
		String apply_staff_name = staff.getStaffName();// 员工名称
		Integer apply_staff_id = staff.getStaffId();// 员工编号
		String org_name = staff.getDeptName();// 申请人组织名称 //sys_org
		Integer org_id = so.getOrgId();// 申请人组织ID //sys_org
		String jybh = staff.getLoginName();// 警员编号
		String org_code = staff.getOrgId() + ""; // sys_org
		// String prov = staff.
		bean.setProv(staff.getProvinceId()); // sys_dis
		bean.setProv_name(staff.getProvince()); // sys_dis
		bean.setEparchy_code(staff.getCityId()); // sys_dis
		bean.setEparchy_name(staff.getCity()); // sys_dis
		bean.setApply_staff_id(String.valueOf(apply_staff_id));
		bean.setApply_staff_name(apply_staff_name);
		bean.setOrg_id(String.valueOf(org_id));
		bean.setOrg_name(org_name);
		bean.setJybh(jybh);
		bean.setOrg_code(org_code);
		return bean;
	}

	@Override
	public int addDzgh(DzghApplyEntity dzghApplyEntity) {
		SysStaff staff = (SysStaff) getSessionCache(R.UserSign.SIGNIN_USER_SESSION);
		String unitId = staff.getUnitId();
		dzghApplyEntity.setUnit_id(unitId);
		this.dzghApplyMapper.addDzgh(dzghApplyEntity);
		String elec_id = dzghApplyEntity.getElec_id();
		String num = dzghApplyEntity.getNum().trim();
		String sel_staff_id = dzghApplyEntity.getSel_staff_id().trim();
		String[] nums = num.split(";");
		String[] sel_staff_ids = sel_staff_id.split(",");
		String in_date = dzghApplyEntity.getApply_date();
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		Map<String, String> m = new HashMap<String, String>();
		for (String s : nums) {
			m.put("elec_id", elec_id);
			m.put("num", s.trim());
			m.put("in_date", in_date);
			list.add(m);
			m = new HashMap<String, String>();
		}
		// 批量导入号码信息(选择手动上传)
		if (!list.isEmpty() && "0".equals(dzghApplyEntity.getImportType()))
			this.dzghApplyMapper.addDzghNum(list);
		list.clear();
		for (String s : sel_staff_ids) {
			m.put("elec_id", elec_id);
			m.put("sel_staff_id", s.trim());
			m.put("in_date", in_date);
			list.add(m);
			m = new HashMap<String, String>();
		}
		// 批量导入员工信息
		this.dzghApplyMapper.addDzghStaff(list);
		return 1;
	}

	public int addDzghNum(List<Map<String, String>> list) {
		return this.dzghApplyMapper.addDzghNum(list);
	}

	public int delDzghNum(String elec_id) {
		return this.dzghApplyMapper.delDzghNum(elec_id);
	}

	public int updateProcess(Map<String, String> m) {
		return this.dzghApplyMapper.updateProcess(m);
	}

	public int addProcessByStep(Map<String, String> m) {
		return this.dzghApplyMapper.addProcessByStep(m);
	}

	public DzghApplyEntity queryByPrimaryKey(DzghApplyEntity entity) {
		return this.dzghApplyMapper.queryByPrimaryKey(entity);
	}

	public int updateDzgh(DzghApplyEntity dzghApplyEntity) {
		this.dzghApplyMapper.updateDzgh(dzghApplyEntity);
		String elec_id = dzghApplyEntity.getElec_id();
		String num = dzghApplyEntity.getNum().trim();
		String sel_staff_id = dzghApplyEntity.getSel_staff_id().trim();
		String[] nums = num.split(";");
		String[] sel_staff_ids = sel_staff_id.split(",");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String in_date = df.format(new Date());
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		Map<String, String> m = new HashMap<String, String>();
		for (String s : nums) {
			m.put("elec_id", elec_id);
			m.put("num", s.trim());
			m.put("in_date", in_date);
			list.add(m);
			m = new HashMap<String, String>();
		}
		// 批量导入号码信息(选择手动上传)
		if (!list.isEmpty() && "0".equals(dzghApplyEntity.getImportType())) {
			this.delDzghNum(elec_id);// 删除之前的号码信息
			this.dzghApplyMapper.addDzghNum(list);
		}
		list.clear();
		for (String s : sel_staff_ids) {
			m.put("elec_id", elec_id);
			m.put("sel_staff_id", s.trim());
			m.put("in_date", in_date);
			list.add(m);
			m = new HashMap<String, String>();
		}
		// 批量导入员工信息
		this.dzghApplyMapper.delDzghStaff(elec_id);
		this.dzghApplyMapper.addDzghStaff(list);
		return 1;
	}

	/*
	 * 根据主键查询电子公函信息 auth：jingrongze
	 * 
	 */
	@Override
	public DzghApplyEntity selectByPrimaryKey(DzghApplyEntity dzghApplyEntity) {
		DzghApplyEntity bean = dzghApplyMapper.selectByPrimaryKey(dzghApplyEntity);
		return bean;
	}

	/*
	 * 根据电子公函编号获取核验号码数量 auth：jingrongze
	 * 
	 */
	@Override
	public int selectCountByPrimaryKey(DzghApplyEntity dzghApplyEntity) {
		return dzghApplyMapper.selectCountByPrimaryKey(dzghApplyEntity);
	}

	/**
	 * @date 2017-5-17
	 * @author 荆荣泽
	 * @desc 查询办案人员ID
	 */
	@Override
	public List getBaId(String elec_id) {
		return dzghApplyMapper.getIdByElecId(elec_id);
	}

	/**
	 * @date 2017-5-17
	 * @author 荆荣泽
	 * @desc 根据ID查询办案人员name
	 */
	@Override
	public String getNameById(String id) {
		return dzghApplyMapper.getNameById(id);
	}

	/**
	 * @date 2017-5-17
	 * @author 荆荣泽
	 * @desc 获取申请人员的姓名
	 */
	@Override
	public String getApplyName(String applyId) {
		return dzghApplyMapper.getNameByApplyId(applyId);
	}

	/**
	 * @date 2017-6-12
	 * @author chenxl
	 * @desc 根据工函查询办案人员
	 */
	@Override
	public List getDoStaffByElecId(String applyId) {
		return dzghApplyMapper.getDoStaffByElecId(applyId);
	}

	/**
	 * @date 2017-6-13
	 * @author chenxl
	 * @desc 根据工函查询详细信息
	 */
	@Override
	public JSONObject getElecApplyInfoByElecId(DzghApplyEntity dzghApplyEntity) {
		// 取基本信息
		DzghApplyEntity bean = dzghApplyMapper.queryByPrimaryKey(dzghApplyEntity);
		// 取办案人员姓名一个或者多个SEL_STAFF_ID
		String elec_id = bean.getElec_id();
		// 查出ID来
		List doStaffList = this.getDoStaffByElecId(elec_id);
		// 查出办案人员name来

		List list = new ArrayList();
		for (int i = 0; i < doStaffList.size(); i++) {
			Map tmp = (Map) doStaffList.get(i);
			tmp.put("staffId", tmp.get("staffId"));
			tmp.put("name", tmp.get("name"));
			list.add(tmp);
		}
		// 取申请人员姓名1个
		String applyId = bean.getApply_staff_id();
		String applyName = this.getApplyName(applyId);
		// 取核验号码数量
		dzghApplyEntity.setElec_id(elec_id);
		JSONObject obj = new JSONObject();
		int count = this.selectCountByPrimaryKey(dzghApplyEntity);
		obj.element("elec_id", bean.getElec_id());
		obj.element("name", bean.getName());
		obj.element("prov", bean.getProv());
		obj.element("eparchy_code", bean.getEparchy_code());
		obj.element("apply_staff_id", bean.getApply_staff_id());
		obj.element("org_id", bean.getOrg_id());
		obj.element("sel_staff_id", bean.getSel_staff_id());
		obj.element("valid_start_date", bean.getValid_start_date());
		obj.element("valid_end_date", bean.getValid_end_date());
		obj.element("apply_desc", bean.getApply_desc());
		obj.element("apply_date", bean.getApply_date());
		obj.element("num", "共有" + count + "条");
		obj.element("doStaffInfo", list);
		obj.element("applyName", applyName);
		obj.element("applyId", applyId);
		obj.element("org_name", bean.getOrg_name());
		obj.element("eparchy_code_name", bean.getEparchy_name());
		obj.element("prov_name", bean.getProv_name());
		obj.element("selectType", bean.getSelectType()==null?"0":bean.getSelectType());

		return obj;
	}
}
